package lik.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

public class LikHttp {

	protected LikHttp() {
		this.cookies = new StringBuilder();
		isOutput = false;
		datas = new StringBuilder();
	}

	protected LikHttp(String url) throws Exception {
		this();
		httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
		httpURLConnection.setConnectTimeout(10000);
		httpURLConnection.setReadTimeout(20000);
		addHeader(LikHttp.USER_AGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");
		addHeader(LikHttp.CONTENT_TYPE, "application/x-www-form-urlencoded ");
		addHeader(LikHttp.ACCEPT, "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*");
		addHeader(LikHttp.CONNECTION, "Keep-Alive");
	}

	public static final String USER_AGENT = "User-Agent";
	public static final String HOST = "host";
	public static final String CONTENT_TYPE = "content-type";
	public static final String ACCEPT = "Accept";
	public static final String CONNECTION = "Connection";
	public static final int GET = 1;
	public static final int POST = 2;
	public static final int PUT = 3;
	public static final int HEAD = 4;
	public static final int OPTIONS = 5;
	public static final int DELETE = 6;
	public static final int TRACE = 7;

	private HttpURLConnection httpURLConnection;
	private StringBuilder cookies;
	private StringBuilder datas;
	private boolean isOutput;

	public static LikHttp initLikHttp(String url) {
		try {
			LikHttp likHttp = new LikHttp(url);
			return likHttp;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public void addDatas(String value) {
		datas.append(value);
	}

	public void setMethod(int method) {
		try {

			switch (method) {
			case 1:
				httpURLConnection.setRequestMethod("GET");
				isOutput = false;
				break;
			case 2:
				httpURLConnection.setRequestMethod("POST");
				isOutput = true;
				break;
			case 3:
				httpURLConnection.setRequestMethod("PUT");
				isOutput = true;
				break;
			case 4:
				httpURLConnection.setRequestMethod("HEAD");
				isOutput = true;
				break;
			case 5:
				httpURLConnection.setRequestMethod("OPTIONS");
				isOutput = true;
				break;
			case 6:
				httpURLConnection.setRequestMethod("DELETE");
				isOutput = true;
				break;
			case 7:
				httpURLConnection.setRequestMethod("TRACE");
				isOutput = true;
				break;
			default:
				System.out.println("error method type");
				break;
			}
		} catch (ProtocolException e) {
			e.printStackTrace();
		}
	}

	public BufferedReader connect() {
		try {
			if (isOutput) {
				addHeader("Content-Length", Integer.toString(datas.toString().length()));
				httpURLConnection.setDoOutput(isOutput);
				httpURLConnection.getOutputStream().write(datas.toString().getBytes());
			}
			return new BufferedReader(new InputStreamReader((InputStream) httpURLConnection.getContent(), "utf-8"));
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public String connectToResult() {
		BufferedReader brReader = null;
		try {
			StringBuilder sBuilder = new StringBuilder();
			brReader = connect();
			while (brReader.ready()) {
				sBuilder.append(brReader.readLine());
				sBuilder.append("\n\r");
			}
			return sBuilder.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (brReader != null) {
				try {
					brReader.close();
					httpURLConnection.disconnect();
				} catch (IOException e) {
				}
			}
		}
	}

	public void addHeader(String name, String value) {
		httpURLConnection.setRequestProperty(name, value);
	}

	public void addCookies(String cookies) {
		this.cookies.append(cookies + ";");
		httpURLConnection.addRequestProperty("cookie", cookies);
	}

	private String findCookie() {
		StringBuilder sbBuilder = new StringBuilder();
		for (int i = 0; httpURLConnection.getHeaderField(i) != null; i++) {
			if ("Set-Cookie".equalsIgnoreCase(httpURLConnection.getHeaderFieldKey(i))) {
				sbBuilder.append(httpURLConnection.getHeaderField(i));
				sbBuilder.append(";");
			}
		}
		if (sbBuilder.length() != 0) {
			sbBuilder.deleteCharAt(sbBuilder.length() - 1);
		}
		return sbBuilder.toString();
	}
}
